<%
# encoding: utf-8
#
# Redmine plugin for Document Management System "Features"
#
# Copyright © 2011-20 Karel Pičman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
%>

<% html_title(l(:dmsf)) %>

<h2><%= l(:link_trash_bin) %></h2>

<div class="dmsf-header">
  <div class="wiki">
    <%= textilizable(@folder ? @folder.description : @project.dmsf_description) %>
  </div>
</div>

<div id="dmsf_ajax_loading" class="ajax-loading"></div>
<%= form_tag(entries_operations_dmsf_path(:id => @project, :folder_id => @folder), :method => :post,
  :class => 'dmsf_entries', :id => 'entries_form', :data => {:cm_url => dmsf_trash_context_menu_path}) do %>
  <%= hidden_field_tag('action') %>
  <div id="dmsf_buttons"  class="dmsf_controls">
    <% if @file_manipulation_allowed && @folder_manipulation_allowed %>
      <%= submit_tag l(:title_restore), :title => l(:title_restore_checked), :name => 'restore_entries',
        :class => 'toggle-selection' %>
      <% if @file_delete_allowed%>
        <%= submit_tag l(:button_delete), :title => l(:title_delete_checked), :name => 'destroy_entries',
          :class => 'toggle-selection', :data => { :confirm => l(:text_are_you_sure) } %>
      <% end %>
    <% end %>
  </div>
  <table class="display list dmsf_list" id="browser">
    <thead>
      <tr>
        <th class="dmsf_checkbox dmsf_th">
          <%= check_box_tag 'check_all', '', false, :class => 'toggle-selection',
                            :title => "#{l(:button_check_all)}/#{l(:button_uncheck_all)}" %>
        </th>
        <% if DmsfFolder.is_column_on?('id') %>
          <th class="dmsf_th">#</th>
        <% end %>
        <% if DmsfFolder.is_column_on?('title') %>
          <th class="dmsf_th"><%= l(:link_title) %></th>
        <% end %>
        <% if DmsfFolder.is_column_on?('extension') %>
            <th class="dmsf_th"><%= l(:link_extension) %></th>
        <% end %>
        <% if DmsfFolder.is_column_on?('size') %>
          <th class="dmsf_th"><%= l(:link_size) %></th>
        <% end %>
        <% if DmsfFolder.is_column_on?('modified') %>
          <th class="dmsf_th"><%= l(:link_modified) %></th>
        <% end %>
        <% if DmsfFolder.is_column_on?('version') %>
          <th title="<%= l(:label_dmsf_version) %>" class="dmsf_th"><%= l(:link_ver) %></th>
        <% end %>
        <% if DmsfFolder.is_column_on?('workflow') %>
          <th class="dmsf_th"><%= l(:link_workflow) %></th>
        <% end %>
        <% if DmsfFolder.is_column_on?('author') %>
          <th class="dmsf_th"><%= l(:link_author) %></th>
        <% end %>
        <% CustomField.where(type: 'DmsfFileRevisionCustomField').order(:position).each do |c| %>
            <% if DmsfFolder.is_column_on?(c.name) %>
                <th class="dmsf_th"><%= h(c.name) %></th>
            <% end %>
        <% end %>
        <th class ="dmsf_th"><%# controls %></th>
        <th class="none"><%# position %></th>
        <th class="none"><%# size calculated %></th>
        <th class="none"><%# modified calculated %></th>
        <th class="none"><%# version calculated %></th>
        <th class="none"><%# clear title %></th>
      </tr>
    </thead>
  <tbody>
  <% classes = 'dir hascontextmenu' %>
  <% @subfolders.each do |subfolder| %>
    <tr class="<%= classes %>">
      <%= render(:partial => 'dir_trash',
        :locals => {
          :project => @project,
          :subfolder => subfolder,
          :link => nil,
          :id => subfolder.id,
          :name => 'folder',
          :title => subfolder.title }) %>
    </tr>
  <% end %>
  <% classes = 'dmsf_gray hascontextmenu' %>
  <% @dir_links.each do |link| %>
    <tr class="<%= classes %>">
      <%= render(:partial => 'dir_trash',
        :locals => {
          :project => link.target_project,
          :subfolder => link.target_folder,
          :link => link,
          :id => link.id,
          :name => 'folder-link',
          :title => link.name }) %>
    </tr>
  <% end %>
  <% classes = 'file hascontextmenu' %>
  <% @files.each do |file| %>
    <% unless file.last_revision %>
      <% Rails.logger.error "Error: dmsf_file id #{file.id} has no revision!" %>
      <% next %>
    <% end %>
    <tr class="<%= classes %>">
      <%= render(:partial => 'file_trash', :locals => {
          :project => @project,
          :file => file,
          :link => nil,
          :id => file.id,
          :name => 'file',
          :title => file.title }) %>
    </tr>
  <% end %>
  <% classes = 'dmsf_gray hascontextmenu' %>
  <% @file_links.each do |link| %>
    <% unless link.target_file %>
        <% Rails.logger.error "Error: dmsf_link id #{link.id} has no target file!" %>
        <% next %>
    <% end %>
    <% unless link.target_file.last_revision %>
      <% Rails.logger.error "Error: dmsf_file id #{link.target_id} has no revision!" %>
      <% next %>
    <% end %>
    <tr class="<%= classes %>">
      <%= render(:partial => 'file_trash', :locals => {
          :project => link.target_project,
          :file => link.target_file,
          :link => link,
          :id => link.id,
          :name => 'file-link',
          :title => link.name }) %>
    </tr>
  <% end %>
  <% @url_links.each do |link| %>
    <tr class="<%= classes %>">
      <%= render(:partial => 'url_trash', :locals => {
       :project => link.target_project,
       :file => link.target_file,
       :link => link,
       :id => link.id,
       :name => 'url-link',
       :title => link.name }) %>
      </tr>
  <% end %>
  </tbody>
  </table>
<% end %>

<%= context_menu %>

<% content_for :header_tags do %>
  <%= stylesheet_link_tag 'jquery.dataTables/datatables.min.css', :plugin => :redmine_dmsf %>
  <%= javascript_include_tag 'jquery.dataTables/datatables.min.js', :plugin => :redmine_dmsf, defer: true %>

  <% title = DmsfFolder.get_column_position('title') %>
  <% position = DmsfFolder.get_column_position('position') %>
  <% commands = DmsfFolder.get_column_position('commands') %>
  <% position = DmsfFolder.get_column_position('position') %>
  <% version = DmsfFolder.get_column_position('version') %>
  <% size_calculated = DmsfFolder.get_column_position('size_calculated') %>
  <% modified_calculated = DmsfFolder.get_column_position('modified_calculated') %>
  <% version_calculated = DmsfFolder.get_column_position('version_calculated') %>
  <% size = DmsfFolder.get_column_position('size') %>
  <% modified = DmsfFolder.get_column_position('modified') %>
  <% clear_title = DmsfFolder.get_column_position('clear_title') %>

  <%= late_javascript_tag do %>
    $('#browser').dataTable({
      orderClasses: false,
      responsive: {
        details: false
      },
      language: {
        url: "<%= plugin_asset_path(:redmine_dmsf, 'javascripts', json_url) %>"
      },
      oLanguage: {
        sSearch: "<%= l(:description_filter) %>:"
      },
      autoWidth: false,
      paginate: false,
      <% if title %>
        order: [[<%= title %>, "asc"]],
      <% end %>
      sortingFixed: [[ <%= position %>, "asc"]],
      columnDefs: [
          { responsivePriority: 1, targets: <%= title %> },
          { responsivePriority: 2, targets: <%= commands %> },
          { searchable: false, targets: [0, <%= commands %>, <%= position %>, <%= size_calculated %>, <%= modified_calculated %>, <%= version_calculated %>, <%= clear_title %>] },
          { sortable: false, targets: [0, <%= commands %>] }
          <% if title %>
            ,{ iDataSort: <%= clear_title %>, targets: [ <%= title %> ] }
          <% end %>
          <% if size %>
            ,{ iDataSort: <%= size_calculated %>, targets: [ <%= size %> ] }
          <% end %>
          <% if modified %>
            ,{ iDataSort: <%= modified_calculated %>, targets: [ <%= modified %> ] }
          <% end %>
          <% if version %>
            ,{ iDataSort: <%= version_calculated %>, targets: [ <%= version %> ] }
          <% end %>
      ],
      "fnInitComplete": function() {
        $("#dmsf_buttons").insertBefore($("#browser_filter"));
        $("#dmsf_ajax_loading").hide();
        $("#browser").show();
        $("#dmsf_buttons").show();
      },
      "fnInfoCallback": function( oSettings, iStart, iEnd, iMax, iTotal, sPre ) {
         return "<%= l(:label_number_of_folders)%>: <%= @subfolders.count + @dir_links.count %>, <%= l(:label_number_of_documents)%>: <%= @files.count + @file_links.count + @url_links.count %>";
      }
    });

    $("#entries_form").submit(function () {
        $(this).removeAttr("data-submitted");
    });
  <% end %>
<% end %>
